import { createRouter, createWebHashHistory } from "vue-router";
import { ElMessage } from "element-plus";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "layout",
      component: () => import("../views/Layoutview.vue"),
      // 路由重定向
      redirect: "/home",

      // 二级路由
      children: [
        {
          path: "/home",
          name: "home",
          component: () => import("../views/HomeView.vue"),
        },
        // {
        //   path: "/productList",
        //   name: "productList",
        //   component: () => import("../views/ProductListView.vue"),
        // },
        {
          path: "/TongView",
          name: "TongView",
          component: () => import("../views/TongView.vue"),
        },
        // {
        //   path: "/userManager",
        //   name: "userManager",
        //   component: () => import("../views/UserManager.vue"),
        // },
        // {
        //   path: "/userAdd",
        //   name: "userAdd",
        //   component: () => import("../views/UserAdd.vue"),
        // },
        {
          path: "/login",
          name: "login",
          component: () => import("../views/loginView.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/loginView.vue"),
    },
  ],
});

//添加路由守卫  判断哪些页面需要登录 哪些写页面不需要登录
//定义一个白名单的数组   这个数组记录哪些页面不需要登录
const whiteList = ["/login"];
router.beforeEach((to) => {
  if (whiteList.includes(to.path) == false) {
    //要访问的页面在白名单中 说明要访问的页面需要登录
    let token = localStorage.getItem("xxMallAdminToken");
    if (token == null) {
      //存在了就已经登录   等于空就是没有登录
      ElMessage({
        message: "还没有登录，进行登录",
        type: "error",
      });
      router.push("/login");
    }
  }
});
export default router;
